package com.example.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.config.UrlsConfig;
import com.example.fruit.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;

/**
 * Created by User on 2015/7/2.
 */
public class BaiKeFragment extends Fragment {

    private HttpUtils httpUtils;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view=inflater.inflate(R.layout.fragment_baike,container,false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        httpUtils=new HttpUtils();
        loadData();
    }

    private void loadData() {
        RequestParams params=getRquestParams();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlsConfig.FIRST_PAGE, params, new RequestCallBack<String>() {


            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.i("info", "nnaa" + responseInfo.result);
                //获得服务器返回的json数据
                try {

//                    JSONArray array = new JSONArray(responseInfo.result);
//                    List<NewFragmentBean> news = JSON.parseArray(array.toString(), NewFragmentBean.class);
//                    data.addAll(news);
//                    adapter.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }



    private RequestParams getRquestParams() {

        RequestParams params = new RequestParams();

        params.addBodyParameter("timestamp", "1435143857");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "");
        params.addBodyParameter("region_id", "143949");
        params.addBodyParameter("service", "bake.getArticleList");
        params.addBodyParameter("device_id", "6ad554bb19874f6d47db3e19b7b69196");
        params.addBodyParameter("channel", "qq");
        params.addBodyParameter("version", "2.1.1");

        params.addBodyParameter("sign", "24b6bc191289dca0d665ced12331707d");
        params.addBodyParameter("limit", "20");
        params.addBodyParameter("page", "1");
        return params;
    }

}
